
local function pingPos(self, pos_from, pos_to)
    if not pos_from or not pos_to then
        self:Hide()
        return
    end
    local dist = pos_from:DistSq(pos_to)
    local lefttime = .1
    if dist > 100 then
	    lefttime = math.max(math.sqrt(dist - 100) / 10, lefttime)
	    self.Transform:SetPosition(pos_from:Get())
	    self.Physics:SetMotorVel(0,0,-10)
        self.Transform:SetRotation(math.atan2(pos_from.x - pos_to.x, pos_from.z - pos_to.z)*180/math.pi)
    end
    self:DoTaskInTime(lefttime, self.Remove)
end

local function ping(self, from, to)
	if not to or not to:IsValid() then self:Hide() return end
    local pos_from = from:GetPosition()
    local pos_to = to:GetPosition()

    self:pingPos(pos_from, pos_to)
    -- 虽然可以这么解决问题但是不推荐
	-- self.Transform:SetRotation(from.Transform:GetRotation())
end

local function collect(self, from, to)
    if not to or not to:IsValid() or not from or not from:IsValid() or not to.prefab
    then
        self:Hide()
        return
    end
    self.to_prefab = to.prefab
    local pos_from = from:GetPosition()
    if self:IsValid() then
        self.Transform:SetPosition(pos_from:Get())
        local pos_to = to:GetPosition()
        self.Transform:SetRotation(math.atan2(pos_from.x - pos_to.x, pos_from.z - pos_to.z)*180/math.pi)
    end
end

local function setColor(self, color)
    self.AnimState:SetMultColour(unpack(PLAYERCOLOURS[string.upper(color)]))
    return self
end


local function standUp(self)
    self:setColor("GREEN")
    local anim = self.AnimState
    anim:SetScale(0.5, 0.5, 0.5)
    anim:SetOrientation(ANIM_ORIENTATION.BillBoard)
    anim:PlayAnimation("beam_marker", true)
    anim:SetDeltaTimeMultiplier(0.2)
    return self
end

local function setParent(self, inst)
    if inst and inst:IsValid() then
        self.entity:SetParent(inst.entity)
    end
    return self
end

local function hide(self)
    self:Hide()
    return self
end

local function show(self)
    self:Show()
    return self
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    local phys = inst.entity:AddPhysics()

    phys:SetMass(2)
	phys:SetFriction(.1)
	phys:SetDamping(0)
	phys:SetSphere(.5)
    phys:ClearCollisionMask()
    phys:CollidesWith(COLLISION.GROUND)


    anim:SetBuild("archive_resonator")
    anim:SetBank("archive_resonator")
    anim:PlayAnimation("beam_marker")
    anim:SetOrientation(ANIM_ORIENTATION.OnGround)
    anim:SetLayer(LAYER_BACKGROUND)
    anim:SetSortOrder(3)
    anim:SetBloomEffectHandle("shaders/anim.ksh")
    anim:SetLightOverride(1)
    -- anim:SetDeltaTimeMultiplier(0.3)

    inst:AddTag("huxi")-- 此标签影响【定位天体】功能, 勿删
    -- inst:AddTag("FX")
    inst:AddTag("NOCLICK")
	inst:AddTag("NOBLOCK")

    inst.ping = ping
    inst.pingPos = pingPos
    inst.setColor = setColor
    inst.standUp = standUp
    inst.collect = collect
    inst.setParent = setParent
    inst.hide = hide
    inst.show = show

    return inst
end


return Prefab("huxi_arrow", fn)